﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BomberManServer.Gaming.Object.Bonuses;
using BomberManServer.Utils;
using System.Configuration;



/// Description :
/// 
/// 

namespace BomberManServer.Gaming.Object.Mapping.Items
{
    public class Avatar : Item
    {
        public UInt32 id { get; private set; }


        // Number of blocks the deflag will reach in one direction
        public UInt32 deflag_length { get; set; }

        //number of maximum active bomb on the map for this avatar
        public UInt32 bomb_max { get; set; }
        
        // delay is the number of action per second who shorten the time required to
        // explode the bomb manualy and lenghtened the forced explosion timer.
        public float bomb_delay { get; set; }

        //private UInt32 _speed;

        public UInt32 speed { get; set; }

        public bool hasKick { get; set; }
        public Coordinates coord {get; set; }
        public List<Block> blocks { get; set; }

        public List<Bomb> bombs { get; set; }
        List<BonusTemp> bonuses;

        public Game game { get; private set; }

        public Modifier modBonus { get; private set; }
        private Match match;
        public Player player { get; private set; }

        public bool isAlive { get; set; }

        public Coordinates lastBlockCoord { get; set; }

        public bool isPlantingBomb { get; set; }

        public Avatar(Player player)
        {
            this.free = false;
            this.destructible = true;
            this.propagation = true;
            this.player = player;
            this.blocks = new List<Block>();
            this.coord = Coordinates.fromPoint(0,0);
            this.speed = player.speed;
            this.deflag_length = player.range;
            this.bomb_max = player.bomb;
            this.bomb_delay = player.delay;
            this.hasKick = false;
            this.bombs = new List<Bomb>();
            this.id = player.id;
            
            this.modBonus = new Modifier(this);
            this.lastBlockCoord = this.coord;
            this.isPlantingBomb = false;
        }

        public override Item actionDeflagration(Deflagration deflagration)
        {
            this.game.ruleKeeper.avatarDie(deflagration.bomb.owner, this);
            return deflagration;
        }

        public void detach()
        {
            foreach (Block block in blocks)
                block.item = Items.None.instance;
            blocks.Clear();
        }
        public void attach(List<Block> currentBlocks)
        {
            blocks = currentBlocks;
            foreach (Block block in blocks)
            {
                block.reactionWalk(this);
                if (!this.isAlive)
                    return;
            }
        }

        public void attach(Block block)
        {
            blocks.Add(block);
            block.reactionWalk(this);
        }

        public void spawn(Block block,UInt32 x_block, UInt32 y_block)
        {
            blocks.Add(block);
            coord.updatePoint(x_block * Constants.instance.pointsPerBlock + Constants.instance.pointsPerBlock / 2, y_block * Constants.instance.pointsPerBlock + Constants.instance.pointsPerBlock / 2);
            this.isAlive = true;
        }

        public void willPlantBomb()
        {
            if (this.bombs.Count < bomb_max)
                isPlantingBomb = true;
            else
                isPlantingBomb = false;
        }

        public void plantBomb(Block block, Coordinates coordBomb)
        {
            block.item = new Bomb(this, coordBomb);
            isPlantingBomb = false;
        }
        public void die()
        {
            this.isAlive = false;
            this.detach();
        }

        public bool triggerBomb()
        {
            if(bombs.Count > 0)
            {
                Bomb bomb = bombs.First();
                bomb.explode();
                return bomb.delayed;
            }
            else
                return false;

        }

        public void initialize(Game game)
        {
            this.game = game;
        }
        
    }
}
